﻿using System;
using System.Collections.Generic;

namespace Zo.MessageQueues
{
    /// <summary>
    /// 消息结果。
    /// </summary>
    public class MessageResult
    {
        private List<MessageOperateError> _errors = new List<MessageOperateError>();

        /// <summary>
        /// 状态。
        /// </summary>
        public bool Status { get; set; }

        /// <summary>
        /// 异常信息。
        /// </summary>
        public Exception Exception { get; set; }

        /// <summary>
        /// 错误信息。
        /// </summary>
        public IEnumerable<MessageOperateError> Errors => _errors;

        /// <summary>
        /// 成功结果。
        /// </summary>
        public static MessageResult Success => new MessageResult { Status = true };

        /// <summary>
        /// 错误结果。
        /// </summary>
        /// <param name="errors"></param>
        /// <returns></returns>
        public static MessageResult Failed(params MessageOperateError[] errors)
        {
            var result = new MessageResult { Status = false };
            if (errors != null)
            {
                result._errors.AddRange(errors);
            }

            return result;
        }

        /// <summary>
        /// 错误结果。
        /// </summary>
        /// <param name="ex"></param>
        /// <param name="errors"></param>
        /// <returns></returns>
        public static MessageResult Failed(Exception ex, params MessageOperateError[] errors)
        {
            var result = new MessageResult
            {
                Status = false,
                Exception = ex
            };
            if (errors != null)
            {
                result._errors.AddRange(errors);
            }

            return result;
        }
    }

    /// <summary>
    /// 消息操作错误信息。
    /// </summary>
    public class MessageOperateError
    {
        /// <summary>
        /// 错误码。
        /// </summary>
        public string Code { get; set; }

        /// <summary>
        /// 错误描述。
        /// </summary>
        public string Description { get; set; }
    }
}